package net.lab1024.sa.admin.module.business.erp.bas.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.sa.admin.module.business.erp.bas.dao.UnitDao;
import net.lab1024.sa.admin.module.business.erp.bas.domain.entity.UnitEntity;
import net.lab1024.sa.admin.module.business.erp.bas.domain.form.UnitAddForm;
import net.lab1024.sa.admin.module.business.erp.bas.domain.form.UnitQueryForm;
import net.lab1024.sa.admin.module.business.erp.bas.domain.form.UnitUpdateForm;
import net.lab1024.sa.admin.module.business.erp.bas.domain.vo.UnitVO;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 单位 Service
 *
 * @Author AKITA
 * @Date 2025-07-23 17:13:13
 * @Copyright AKITA
 */

@Service
public class UnitService {

    @Resource
    private UnitDao unitDao;

    /**
     * 分页查询
     */
    public PageResult<UnitVO> queryPage(UnitQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<UnitVO> list = unitDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    /**
     * 添加
     */
    public ResponseDTO<String> add(UnitAddForm addForm) {
        UnitEntity unitEntity = SmartBeanUtil.copy(addForm, UnitEntity.class);
        unitDao.insert(unitEntity);
        return ResponseDTO.ok();
    }

    /**
     * 更新
     */
    public ResponseDTO<String> update(UnitUpdateForm updateForm) {
        UnitEntity unitEntity = SmartBeanUtil.copy(updateForm, UnitEntity.class);
        unitDao.updateById(unitEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     */
    public ResponseDTO<String> batchDelete(List<String> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return ResponseDTO.ok();
        }

        unitDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(String id) {
        if (null == id) {
            return ResponseDTO.ok();
        }

        unitDao.deleteById(id);
        return ResponseDTO.ok();
    }
}
